﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using GDoc.Data;

namespace GDoc.Domain.Services
{
    public class UserService
    {
        public bool ValidateUser(string email, string password)
        {
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            User user = GetByEmail(email);

            if (user == null)
                return false;

            return user.Password.Equals(password);
        }

        public User GetByEmail(string email)
        {
            IUserRepository repository = RepositoryFactory.CreateUserRepository();

            User user = repository.GetByEmail(email);

            return user;
        }

        public User CreateUser(string email, string password)
        {
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            User user = new User
            {
                Email = email,
                Password = password
            };

            IUserRepository repository = RepositoryFactory.CreateUserRepository();

            repository.SaveOrUpdate(user);

            return user;
        }

        public bool ChangePassword(string email, string oldPassword, string newPassword)
        {
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword");
            if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword");
            if (newPassword.Equals(oldPassword)) throw new ArgumentException("New password and Old password cannot be the same.", "newPassword");

            // The underlying ChangePassword() will throw an exception rather
            // than return false in certain failure scenarios.
            User currentUser = GetByEmail(email);

            if (currentUser == null)
                throw new ArgumentException("User not found. Email: " + email); ;

            if (!currentUser.Password.Equals(oldPassword))
                return false;

            currentUser.Password = newPassword;

            IUserRepository repository = RepositoryFactory.CreateUserRepository();

            repository.SaveOrUpdate(currentUser);

            return true;
        }

        public User Update(User user)
        {
            IUserRepository repository = RepositoryFactory.CreateUserRepository();

            return repository.SaveOrUpdate(user);
        }
    }
}
